<template>
  <div class="resetPassword">
    <header class="flex-between">
      <div class="leftDesc flex-start">
        <div class="logo"></div>
        <div class="system">{{ systemName }}</div>
      </div>
      <div class="rightDesc flex-start">
        <div class="backBtn cp" @click="goBack">返回</div>
      </div>
    </header>
    <div class="content">
      <div class="header">
        <span>修改密码</span>
        <div class="tan"></div>
      </div>
      <div class="innerContent">
        <div class="steps">
          <step-line v-for="(item, index) in stepList" :key="index" :name="item" :step="index + 1" :width="'long'" :finished="index <= currentIndex"></step-line>
        </div>
        <form1 v-if="currentIndex === 0"></form1>
        <form2 v-if="currentIndex === 1"></form2>
        <form3 v-if="currentIndex === 2"></form3>
        <div class="btnBox">
          <div class="nextbtn" @click="nextStep">{{ btnName }}</div>
        </div>
      </div>
    </div>
    <img-check-dialog ref="imgDialog"></img-check-dialog>
  </div>
</template>

<script>
import form1 from './components/form1'
import form2 from './components/form2'
import form3 from './components/form3'
export default {
  data () {
    return {
      systemName: '福讯会议系统',
      currentIndex: 0, // 当前步骤
      stepList: [
        '手机号验证',
        '重置密码',
        '完成'
      ],
      btnName: '下一步'
    }
  },
  computed: {
    // 判断图形验证码组件显示隐藏
    showImgDialog () {
      return this.$store.state.common.showImgDialog
    },
    // 手机号
    phone () {
      return this.$store.state.user.userInfo.user_phone
    }
  },
  watch: {
    currentIndex: {
      handler (val) {
        this.stepList.forEach((item, index) => {
          switch (val) {
            case 1:
              this.btnName = '确认'
              break
            case 2:
              this.btnName = '完成'
              break
            default:
              this.btnName = '下一步'
              break
          }
        })
      }
    },
    phone: {
      deep: true,
      immediate: true,
      handler (val) {
        if (val) {
          this.$store.commit('findPassword/set_phone', val)
        }
      }
    },
    showImgDialog: {
      immediate: true,
      handler (val) {
        if (val) {
          this.$refs.imgDialog.getPhone(this.phone)
        }
      }
    }
  },
  components: {
    form1,
    form2,
    form3
  },
  methods: {
    // 返回
    goBack () {
      const path = this.tools.storage.getBackPage()

      this.$router.push(path)
      this.tools.storage.removeBackPage()
    },
    // 点击下一步
    async nextStep () {
      switch (this.currentIndex) {
        case 0: {
          await this.$store.dispatch('findPassword/checkPhoneCode').then(res => {
            if (res.data.code === 200) {
              this.currentIndex++
              this.$store.commit('common/clear_timer')
              this.$store.commit('common/set_countDownText', '获取验证码')
            }
          })
          break
        }
        case 1: {
          this.$store.dispatch('findPassword/changePassword').then(res => {
            if (res) {
              if (res.data.code === 200) {
                this.currentIndex++
              }
            }
          })
          break
        }
        case 2: {
          this.goBack()
          break
        }
      }
    }
  },
  created () {
    this.$store.commit('findPassword/set_state')
  }
}
</script>

<style scoped lang="scss">
.resetPassword {
  width: 100%;
  height: 100%;
  background-color: #F6F7FB;

  header {
    width: 1200px;
    height: 88px;
    border-bottom: 1px solid #D8DEE9;
    margin: 0 auto;

    .leftDesc {
      .logo {
        width: 48px;
        height: 48px;
        background: url("../../assets/Login/images/logo.png") no-repeat;
        background-position: center center;
        background-size: 100% 100%;
      }

      .system {
        margin-left: 12px;
        height: 33px;
        font-size: 24px;
        font-weight: 600;
        text-align: left;
        color: #304eec;
        line-height: 33px;
        letter-spacing: 1px;
      }
    }
    .rightDesc {
      .backBtn {
        margin-left: 12px;
        width: 57px;
        height: 32px;
        border: 1px solid #304eec;
        border-radius: 2px;
        font-size: 14px;
        font-weight: 400;
        text-align: center;
        color: #304eec;
        line-height: 32px;
        letter-spacing: 0px;
      }
    }
  }

  .content {
    width: 1200px;
    margin: 24px auto 0;
    height: 640px;
    background-color: #fff;
    box-shadow: 0px 6px 36px 0px #EBEDF3;
    user-select: none;

    .header {
      position: relative;
      height: 60px;
      background: #304eec;
      font-size: 18px;
      font-weight: 500;
      color: #ffffff;
      line-height: 60px;
      letter-spacing: 1px;
      text-align: center;
      margin-bottom: 62px;
      .tan {
        position: absolute;
        left: 50%;
        bottom: -12px;
        transform: translateX(-50%);
        width: 12px;
        height: 12px;
        border-top: 6px solid #304eec;
        border-left: 6px solid transparent;
        border-right: 6px solid transparent;
        border-bottom: 6px solid transparent;
      }
    }

    .innerContent {
      width: 540px;
      height: 350px;
      margin: 0 auto;
      .steps {
        height: 110px;
      }
      .btnBox{
        margin: 0 auto;
        width: 400px;
        .nextbtn{
          height: 48px;
          text-align: center;
          line-height: 48px;
          background: linear-gradient(316deg,#4a53ef, #6971fe 100%);
          border-radius: 2px;
          color: #ffffff;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
